import 'package:base_widgets/export.dart';
import 'package:flutter/material.dart';
import 'package:toast/toast.dart';
import '../../constant/app_router.dart';
import '/common/common_util.dart';
import '../register/page_register.dart';
import '/common/widget_factory.dart';
import 'login_change_notifier.dart';

class LoginPage extends StatefulWidget {
  const LoginPage({super.key});

  @override
  State<StatefulWidget> createState() {
    return LoginPageState();
  }
}

class LoginPageState extends State<LoginPage> {
  final TextEditingController _accountEditingController = TextEditingController();
  final TextEditingController _passwordEditingController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("登录"),
      ),
      body: Stack(
        alignment: AlignmentDirectional.center,
        children: <Widget>[
          Positioned(
              top: 200,
              height: 260,
              width: 300,
              child: Column(
                children: <Widget>[
                  WidgetFactor.buildTextField(
                      controller: _accountEditingController, hintText: "请输入账号", obscureText: false),
                  Flexible(
                    flex: 1,
                    child: Container(),
                  ),
                  WidgetFactor.buildTextField(
                      controller: _passwordEditingController, hintText: "请输入密码", obscureText: true),
                  Flexible(
                    flex: 3,
                    child: Container(),
                  ),
                  Consumer(builder: (context, LoginChangeNotifier loginChangeNotifier, child) {
                    return WidgetFactor.buildButton(
                      onPressed: () => _clickLoginBtn(context, loginChangeNotifier),
                      title: "登录",
                    );
                  }),
                  Flexible(
                    flex: 1,
                    child: Container(),
                  ),
                  _buildRegisterBtn(),
                ],
              )),
        ],
      ),
    );
  }

  Widget _buildRegisterBtn() {
    return TextButton(
        onPressed: _clickRegisterBtn,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: const <Widget>[
            Text(
              "还没账号?",
              style: TextStyle(fontSize: 14),
            ),
            Text(
              "注册",
              style: TextStyle(fontSize: 14, color: Colors.blue),
            ),
          ],
        ));
  }

  void _clickLoginBtn(
    BuildContext context,
    LoginChangeNotifier loginChangeNotifier,
  ) {
    if (_accountEditingController.text.trim().isEmpty) {
      Toast.show("请先输入账号");
    } else if (_passwordEditingController.text.trim().isEmpty) {
      Toast.show("请先输入密码");
    } else {
      _login(
        context,
        loginChangeNotifier,
        _accountEditingController.text.trim(),
        _passwordEditingController.text.trim(),
      );
    }
  }

  void _login(
    BuildContext context,
    LoginChangeNotifier loginChangeNotifier,
    String username,
    String password,
  ) async {
    bool success = await loginChangeNotifier.login(context, username, password);
    if (success && mounted) {
      Toast.show("登录成功");
      Navigator.of(context).pop();
    }
  }

  void _clickRegisterBtn() {
    CommonUtil.jumpToPageByPageName(context, AppRouter.register);
  }

  @override
  void dispose() {
    _accountEditingController.dispose();
    _passwordEditingController.dispose();
    super.dispose();
  }
}
